

package com.kimo.shop.delivery.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.kimo.shop.bean.enums.OfflineHandleEventType;
import com.kimo.shop.bean.model.OfflineHandleEvent;
import com.kimo.shop.bean.model.Order;
import com.kimo.shop.bean.model.Station;
import com.kimo.shop.bean.param.OfflineHandleEventAuditParam;
import com.kimo.shop.common.annotation.SysLog;
import com.kimo.shop.common.config.Constant;
import com.kimo.shop.common.exception.KimoShopBindException;
import com.kimo.shop.common.util.PageParam;
import com.kimo.shop.delivery.comment.model.OrderSelfStation;
import com.kimo.shop.delivery.comment.service.OrderSelfStationService;
import com.kimo.shop.service.OfflineHandleEventService;
import com.kimo.shop.service.OrderService;
import com.kimo.shop.service.StationService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 自提点
 *
 * @author kimo-taozhouchuan
 * @date 2020-04-23 15:18:29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/station" )
public class StationController {

    private final StationService stationService;

    private final OrderSelfStationService orderSelfStationService;

    private final OfflineHandleEventService offlineHandleEventService;

    private final OrderService orderService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param station 自提点
     * @return 分页数据
     */
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin:station:page')" )
    public ResponseEntity<IPage<Station>> getStatonPage(PageParam<Station> page, Station station) {
        return ResponseEntity.ok(stationService.getPage(page,station));
    }


    /**
     * 通过id查询自提点
     * @param stationId id
     * @return 单个数据
     */
    @GetMapping("/info/{stationId}" )
    @PreAuthorize("@pms.hasPermission('admin:station:info')" )
    public ResponseEntity<Station> getById(@PathVariable("stationId") Long stationId) {
        return ResponseEntity.ok(stationService.getStationInfoById(stationId));
    }

    /**
     * 新增自提点
     * @param station 自提点
     * @return 是否新增成功
     */
    @SysLog("新增自提点" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin:station:save')" )
    public ResponseEntity<Boolean> save(@RequestBody @Valid Station station) {
        station.setShopId(Constant.PLATFORM_SHOP_ID);
        stationService.insertStation(station);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改自提点
     * @param station 自提点
     * @return 是否修改成功
     */
    @SysLog("修改自提点" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin:station:update')" )
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid Station station) {
        stationService.updateStation(station);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过id删除自提点
     * @param stationId id
     * @return 是否删除成功
     */
    @SysLog("删除自提点" )
    @DeleteMapping("/{stationId}" )
    @PreAuthorize("@pms.hasPermission('admin:station:delete')" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long stationId) {
        stationService.removeStationById(stationId);
        return ResponseEntity.ok().build();
    }


    /**
     * 账号密码设置
     * @param station 自提点
     * @return 是否修改成功
     */
    @SysLog("自提点账号密码设置" )
    @PutMapping("/changeAccountInfo" )
    @PreAuthorize("@pms.hasPermission('admin:station:update')" )
    public ResponseEntity<Void> changeAccountInfo(@RequestBody @Valid Station station) {
        stationService.changeAccountInfo(station);
        return ResponseEntity.ok().build();
    }

    /**
     * 校验账号
     * @param stationId
     * @param account
     * @return
     */
    @GetMapping("/checkStationAccount" )
    public ResponseEntity<Boolean> checkStationAccount(@RequestParam("stationId")Long stationId, @RequestParam("account")String account) {
        int count = stationService.getCountByAccount(account,stationId);
        if (count > 0){
            return ResponseEntity.ok(false);
        }
        return ResponseEntity.ok(true);
    }


    /**
     * 通过自提点id获取下线信息
     */
    @GetMapping("/getOfflineHandleEventByStatonId/{stationId}")
    public ResponseEntity<OfflineHandleEvent> getOfflineHandleEventByStatonId(@PathVariable("stationId") Long stationId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.STATION.getValue(), stationId);
        return ResponseEntity.ok(offlineHandleEvent);
    }

    /**
     * 违规商品提交审核
     */
    @PostMapping("/auditApply")
    public ResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Station station = stationService.getById(offlineHandleEventAuditParam.getHandleId());
        if (station == null) {
            // 未找到该自提点信息
            throw new KimoShopBindException("kimo.station.no.find");
        }
        stationService.auditApply(offlineHandleEventAuditParam.getEventId(), offlineHandleEventAuditParam.getHandleId(), offlineHandleEventAuditParam.getReapplyReason());

        // 移除缓存
//        seckillService.removeSeckillCacheById(seckill.getSeckillId());
        return ResponseEntity.ok().build();
    }

    /**
     * 获取提货订单详情
     */
    @GetMapping("/getOrderItemAndStationInfo")
    public ResponseEntity<OrderSelfStation> getOrderItemAndStationInfo(@RequestParam("orderNumber") String orderNumber) {
        Order order = orderService.getOrderAndOrderItemByOrderNumber(orderNumber);
        OrderSelfStation orderSelfStation = orderSelfStationService.getOne(new LambdaQueryWrapper<OrderSelfStation>().eq(OrderSelfStation::getOrderNumber, orderNumber));
        orderService.getOrderItenRefundStatus(order);
        orderSelfStation.setOrderItems(order.getOrderItems());
        return ResponseEntity.ok(orderSelfStation);
    }

    /**
     * 自提订单发货
     */
    @PutMapping("/changeStatusAndOrderStation")
    public ResponseEntity<Void> changeStatusAndOrderStation(@RequestBody OrderSelfStation orderSelfStation) {
        List<String> orderNumberList = Lists.newArrayList();
        orderNumberList.add(orderSelfStation.getOrderNumber());
        orderService.orderStationByOrderNumber(orderNumberList,null, orderSelfStation.getStationId());
        return ResponseEntity.ok().build();
    }
}
